/**
 * 
 */
package hcmus.fit.master.sevensea.view;

import hcmus.fit.master.sevensea.R;
import hcmus.fit.master.sevensea.background.Sea;
import hcmus.fit.master.sevensea.control.Control;
import hcmus.fit.master.sevensea.core.AbstractView;
import hcmus.fit.master.sevensea.core.Constant;
import hcmus.fit.master.sevensea.sprite.ship.ShipState;
import hcmus.fit.master.sevensea.util.GameUtils;
import hcmus.fit.master.sevensea.util.Size;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

/**
 * @author hvp
 * 
 */
public class PlayView extends AbstractView {

    // private boolean running;
    private Sea seamap;
    private Control control;
    private int difficult;
    // private Bitmap gameOverBitmap;
    private Bitmap bufferBitmap;
    private Canvas bufferCanvas;
    private Size viewSize;

    public PlayView(Context context) {
        super(context);
        viewSize = GameUtils.getScreenSize(context);
        // viewSize = new Size(viewSize.getWidth(), viewSize.getWidth());
        // bufferBitmap = Bitmap.createBitmap(viewSize.getWidth(),
        // viewSize.getHeight(),
        // Constant.BITMAP_CONFIG);
        bufferBitmap = Bitmap.createBitmap(viewSize.getWidth(), viewSize.getHeight(),
                Constant.BITMAP_CONFIG);
        bufferCanvas = new Canvas(bufferBitmap);
        init(viewSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        bufferBitmap.eraseColor(Color.BLACK);
        super.onDraw(bufferCanvas);
        render(bufferCanvas);
        canvas.drawBitmap(bufferBitmap, 0, 0, null);
    }

    @Override
    public void update() {
        if (seamap == null || seamap.items == null) {
            return;
        }
        seamap.update();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            pointerReleased((int) event.getX(), (int) event.getY());
        }
        return super.onTouchEvent(event);
    }

    public void init(Size size) {
        InputStream is = this.getResources().openRawResource(R.raw.level);
        StringBuffer sb = new StringBuffer();
        try {
            int chars;
            while ((chars = is.read()) != -1) {
                sb.append((char) chars);
            }
        } catch (Exception e) {
        }

        Sea.levelInfomation = GameUtils.getLevelInformation(sb.toString());
        GameUtils.loadResource(getResources());
        Control.LoadResources(getResources());

        // running = true;
        seamap = new Sea(new Size(Constant.MAP_WIDTH, Constant.MAP_HEIGHT), 10 * difficult);
        control = new Control(size);
        seamap.boardControl = control;
    }

    public void stop() {
        // running = false;
        Sea.UnLoadResource();
    }

    public void render(Canvas canvas) {
        seamap.draw(canvas);
        control.Show(canvas);
        Bitmap img = null;
        if (seamap.gameOver) {

            img = BitmapFactory.decodeResource(getResources(), R.drawable.gameover);
            canvas.drawBitmap(img, 45, 45, new Paint());
        } else {
            if (seamap.levelComplete) {
                img = BitmapFactory.decodeResource(getResources(), R.drawable.dialog);
                canvas.drawBitmap(img, 40, 80, new Paint());
            } else if (seamap.busy <= 0) {
                if (seamap.win()) {
                    if (seamap.close()) {
                        if (seamap.boardControl._Life.LIFE <= 1) {
                            seamap.gameOver();
                            return;
                        } else {
                            seamap.boardControl._Life.LIFE--;
                        }
                    }
                    seamap.nextLevel();
                    return;
                }
                if (seamap.close()) {
                    if (seamap.boardControl._Life.LIFE <= 1) {
                        seamap.gameOver();
                    } else {
                        seamap.boardControl._Life.LIFE--;
                        seamap.reloadCurrentLevel();
                    }
                    return;
                }
            }
        }
    }

    public void pointerReleased(int x, int y) {
        // if (x > 53 && y > 154 && x < 196 && y < 178) {
        // if (_seamap.LevelComplete) {
        // _seamap.ChangeLevel();
        // return;
        // }
        // }
        // if (x > 177 && y > 293 && x < 214 && y < 305) {
        // // exit
        // running = false;
        // // mainmidlet.runMainMenuCanvas(difficult);
        // }
        // if (_seamap.Busy > 0)
        // return;
        // if (x > 30 && y > 293 && x < 67 && y < 303) {
        // // undo
        // _seamap.SetFromBackUp();
        // }

        seamap.busy++;

        int tmp = seamap.mainShip.GetDirection(x, y, viewSize);

        if (tmp >= -1) {
            seamap.backUpAll();

            if (tmp == -1) {
                seamap.mainShip.Shoot(seamap.map);
            } else {
                seamap.mainShip.GetDestination();
                seamap.mainShip.State = ShipState.Rotating;
            }
        } else {
            seamap.busy--;
        }
    }
}
